'use strict';

const Controller = require('egg').Controller;

class NftController extends Controller {
  // 查询NFT的双重签名是否使用
  async signHashStatusNft() {
    const { app, ctx, service } = this;
    const messageHash = ctx.request.body.messageHash;
    const res = await service.nft.signHashStatusNft2(messageHash);
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 查询NFT的orderId是否使用
  async orderIdStatusNft() {
    const { app, ctx, service } = this;
    const orderId = ctx.request.body.orderId;
    const res = await service.nft.orderIdMapping2(orderId);
    ctx.body = {code: 1000, msg: '请求成功', data: res};
  }

  // 开盲盒的二次签名;
  async openBoxSecondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);


    // 验证的数据有: 拥有者地址, tokenId, 消耗的HNFT数量, 随机数, orderId;
    const userAddress = ctx.request.body.userAddress;
    const tokenId = ctx.request.body.tokenId;
    const hnftNumber = ctx.request.body.hnftNumber;
    const nonce = ctx.request.body.nonce;
    const orderId = ctx.request.body.orderId;

    // 验证地址
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // 金额必须大于0, 不能有小数
    if(ctx.helper.lte(hnftNumber, '0') || ctx.helper.isDot(hnftNumber)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(tokenId, '0') || ctx.helper.isDot(tokenId)) {
      ctx.body = { code: 1001, msg: 'tokenId不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nonce, '0') || ctx.helper.isDot(nonce)) {
      ctx.body = { code: 1001, msg: 'nonce不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(orderId, '0') || ctx.helper.isDot(orderId)) {
      ctx.body = { code: 1001, msg: 'orderId不能小于0, 不能是小数', data: {} };
      return;
    }


    const userAddress0x = await service.eth.addressToHex2(userAddress);
    // 开始签名
    const res = await service.nft.openBoxSecondSignature2(
      userAddress0x, tokenId, hnftNumber, nonce, orderId
    );
    const data = {
      // 默认地址
      userAddress: userAddress,
      // 转换之后的地址
      userAddress0x: userAddress0x,
      // 其它值
      tokenId: tokenId,
      hnftNumber: hnftNumber,
      nonce: nonce,
      orderId: orderId,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };

    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 合成的二次签名;
  async joinBoxSecondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);


    // 验证的数据有: 拥有者地址, 4个需要销毁的tokenId, 新的tokenId, 随机数;
    const userAddress = ctx.request.body.userAddress;
    const tokenId1 = ctx.request.body.tokenId1;
    const tokenId2 = ctx.request.body.tokenId2;
    const tokenId3 = ctx.request.body.tokenId3;
    const tokenId4 = ctx.request.body.tokenId4;
    const newTokenId = ctx.request.body.newTokenId;
    const nonce = ctx.request.body.nonce;

    // 验证地址
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // tokenId必须大于0, 不能有小数
    if(ctx.helper.lte(tokenId1, '0') || ctx.helper.isDot(tokenId1)) {
      ctx.body = { code: 1001, msg: 'tokenId1不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(tokenId2, '0') || ctx.helper.isDot(tokenId2)) {
      ctx.body = { code: 1001, msg: 'tokenId2不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(tokenId3, '0') || ctx.helper.isDot(tokenId3)) {
      ctx.body = { code: 1001, msg: 'tokenId3不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(tokenId4, '0') || ctx.helper.isDot(tokenId4)) {
      ctx.body = { code: 1001, msg: 'tokenId4不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(newTokenId, '0') || ctx.helper.isDot(newTokenId)) {
      ctx.body = { code: 1001, msg: 'newTokenId不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nonce, '0') || ctx.helper.isDot(nonce)) {
      ctx.body = { code: 1001, msg: 'nonce不能小于0, 不能是小数', data: {} };
      return;
    }


    const userAddress0x = await service.eth.addressToHex2(userAddress);
    // 开始签名
    const res = await service.nft.joinBoxSecondSignature2(
      userAddress0x, tokenId1, tokenId2, tokenId3, tokenId4, newTokenId, nonce
    );
    const data = {
      // 默认地址
      userAddress: userAddress,
      // 转换之后的地址
      userAddress0x: userAddress0x,
      // 其它值
      tokenId1: tokenId1,
      tokenId2: tokenId2,
      tokenId3: tokenId3,
      tokenId4: tokenId4,
      newTokenId: newTokenId,
      nonce: nonce,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };

    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 市场购买交易的二次签名;
  async marketBuyTokenSecondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);


    // 验证的数据有: 卖方地址, 买方地址, 卖方获得的波场数量, 合约获得的波场数量, tokenId, 随机数, orderId;
    const sellerAddress = ctx.request.body.sellerAddress;
    const buyAddress = ctx.request.body.buyAddress;
    const sellerTrxValue = ctx.request.body.sellerTrxValue;
    const contractTrxValue = ctx.request.body.contractTrxValue;
    const tokenId = ctx.request.body.tokenId;
    const nonce = ctx.request.body.nonce;
    const orderId = ctx.request.body.orderId;

    // 验证地址
    if(service.eth.isAddress(sellerAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    if(service.eth.isAddress(buyAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // tokenId必须大于0, 不能有小数
    if(ctx.helper.lte(sellerTrxValue, '0') || ctx.helper.isDot(sellerTrxValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(contractTrxValue, '0') || ctx.helper.isDot(contractTrxValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(tokenId, '0') || ctx.helper.isDot(tokenId)) {
      ctx.body = { code: 1001, msg: 'tokenId不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nonce, '0') || ctx.helper.isDot(nonce)) {
      ctx.body = { code: 1001, msg: 'nonce不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(orderId, '0') || ctx.helper.isDot(orderId)) {
      ctx.body = { code: 1001, msg: 'orderId不能小于0, 不能是小数', data: {} };
      return;
    }

    const sellerAddress0x = await service.eth.addressToHex2(sellerAddress);
    const buyAddress0x = await service.eth.addressToHex2(buyAddress);
    // 开始签名
    const res = await service.nft.marketBuyTokenSecondSignature2(
      sellerAddress0x, buyAddress0x, sellerTrxValue, contractTrxValue, tokenId, nonce, orderId
    );
    const data = {
      // 默认地址
      sellerAddress: sellerAddress,
      buyAddress: buyAddress,
      // 转换之后的地址
      sellerAddress0x: sellerAddress0x,
      buyAddress0x: buyAddress0x,
      // 其它值
      sellerTrxValue: sellerTrxValue,
      contractTrxValue: contractTrxValue,
      tokenId: tokenId,
      nonce: nonce,
      orderId: orderId,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };

    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 首发购买交易的二次签名;
  async nipoBuyTokenSecondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);

    // 验证的数据有: 卖方地址, 买方地址, 卖方获得的波场数量, 合约获得的波场数量, 兑换进行销毁的波场数量, tokenId, 随机数, orderId;
    let sellerAddress = ctx.request.body.sellerAddress;
    const buyAddress = ctx.request.body.buyAddress;
    const sellerTrxValue = ctx.request.body.sellerTrxValue;
    const contractTrxValue = ctx.request.body.contractTrxValue;
    const burnTrxValue = ctx.request.body.burnTrxValue;
    const tokenId = ctx.request.body.tokenId;
    const nonce = ctx.request.body.nonce;
    const orderId = ctx.request.body.orderId;
    // 卖方为设计师地址
    sellerAddress = app.BSC.StylistAddress;

    // 验证地址
    if(service.eth.isAddress(sellerAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    if(service.eth.isAddress(buyAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // tokenId必须大于0, 不能有小数
    if(ctx.helper.lte(sellerTrxValue, '0') || ctx.helper.isDot(sellerTrxValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(contractTrxValue, '0') || ctx.helper.isDot(contractTrxValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(burnTrxValue, '0') || ctx.helper.isDot(burnTrxValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(tokenId, '0') || ctx.helper.isDot(tokenId)) {
      ctx.body = { code: 1001, msg: 'tokenId不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nonce, '0') || ctx.helper.isDot(nonce)) {
      ctx.body = { code: 1001, msg: 'nonce不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(orderId, '0') || ctx.helper.isDot(orderId)) {
      ctx.body = { code: 1001, msg: 'orderId不能小于0, 不能是小数', data: {} };
      return;
    }


    const sellerAddress0x = await service.eth.addressToHex2(sellerAddress);
    const buyAddress0x = await service.eth.addressToHex2(buyAddress);
    // 开始签名
    const res = await service.nft.nipoBuyTokenSecondSignature2(
      sellerAddress0x, buyAddress0x, sellerTrxValue, contractTrxValue, burnTrxValue, tokenId, nonce, orderId
    );
    const data = {
      // 默认地址
      sellerAddress: sellerAddress,
      buyAddress: buyAddress,
      // 转换之后的地址
      sellerAddress0x: sellerAddress0x,
      buyAddress0x: buyAddress0x,
      // 其它值
      sellerTrxValue: sellerTrxValue,
      contractTrxValue: contractTrxValue,
      burnTrxValue: burnTrxValue,
      tokenId: tokenId,
      nonce: nonce,
      orderId: orderId,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };

    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 领取NFT挖矿收益的二次签名
  async drawRedSecondSignature() {
    const { app, ctx, service } = this;
    // ctx.logger.info(ctx.request.body);


    // 验证的数据有: 领取者地址, 领取的数量, 随机数;
    const userAddress = ctx.request.body.userAddress;
    const trxValue = ctx.request.body.trxValue;
    const nonce = ctx.request.body.nonce;

    // 验证地址
    if(service.eth.isAddress(userAddress) === false) {
      ctx.body = { code: 1001, msg: '这个地址不合法', data: {} };
      return;
    }
    // tokenId必须大于0, 不能有小数
    if(ctx.helper.lte(trxValue, '0') || ctx.helper.isDot(trxValue)) {
      ctx.body = { code: 1001, msg: '金额不能小于0, 不能是小数', data: {} };
      return;
    }
    if(ctx.helper.lte(nonce, '0') || ctx.helper.isDot(nonce)) {
      ctx.body = { code: 1001, msg: 'nonce不能小于0, 不能是小数', data: {} };
      return;
    }

    const userAddress0x = await service.eth.addressToHex2(userAddress);
    // 开始签名
    const res = await service.nft.drawRedSecondSignature2(userAddress0x, trxValue, nonce);
    const data = {
      // 默认地址
      userAddress: userAddress,
      // 转换之后的地址
      userAddress0x: userAddress0x,
      // 其它值
      trxValue: trxValue,
      nonce: nonce,
      // 签名的数据
      messageHash: res.messageHash,
      signature: res.signature
    };
    ctx.body = {code: 1000, msg: '请求成功', data: data};
  }

  // 监听交易事件
  async transferEvent() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Transfer';
    const res = await service.nft.scanNftEvent2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        from0x: res[i].result.from,
        to0x: res[i].result.to,
        tokenId: res[i].result.tokenId,
        from: await service.eth.addressFromHex(res[i].result.from),
        to: await service.eth.addressFromHex(res[i].result.to),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 监听铸造事件;
  async mintEvent() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Mint';
    const res = await service.nft.scanNftEvent2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        owner0x: res[i].result.owner,
        tokenId: res[i].result.tokenId,
        owner: await service.eth.addressFromHex(res[i].result.owner),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 监听合成事件
  async joinEvent() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Join';
    const res = await service.nft.scanNftEvent2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        owner0x: res[i].result.owner,
        tokenId1: res[i].result.tokenId1,
        tokenId2: res[i].result.tokenId2,
        tokenId3: res[i].result.tokenId3,
        tokenId4: res[i].result.tokenId4,
        newTokenId: res[i].result.newTokenId,
        owner: await service.eth.addressFromHex(res[i].result.owner),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 监听购买事件;
  async buyEvent() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Buy';
    const res = await service.nft.scanNftEvent2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        seller0x: res[i].result.seller,
        buyer0x: res[i].result.buyer,
        tokenId: res[i].result.tokenId,
        value: res[i].result.value,
        nonce: res[i].result.nonce,
        seller: await service.eth.addressFromHex(res[i].result.seller),
        buyer: await service.eth.addressFromHex(res[i].result.buyer),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 监听购买首发事件;
  async buyNipoEvent() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'BuyNipo';
    const res = await service.nft.scanNftEvent2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        seller0x: res[i].result.seller,
        buyer0x: res[i].result.buyer,
        tokenId: res[i].result.tokenId,
        value: res[i].result.value,
        nonce: res[i].result.nonce,
        seller: await service.eth.addressFromHex(res[i].result.seller),
        buyer: await service.eth.addressFromHex(res[i].result.buyer),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }



}

module.exports = NftController;
